<?php
class Calendar
{
	protected $year;
	protected $month;
	protected $monthUnixTime;
	
	public function __construct($year,$month)
	{
		$this->year = $year;
		$this->month = $month;
		$this->monthUnixTime = mktime(0,0,0,$this->month,1,$this->year);
	}
	
	/*
	*Create array with information about appintments in month
	*
	*
	*return array: data with appointments in month
	*/
	public function getMonth()
	{
		$this->countDays = date('t',$this->monthUnixTime);
		for($i = 1; $i <= $this->countDays; $i++)
		{
			$this->day = mktime(0,0,0,$this->month,$i,$this->year);
			$this->days[$i]['number'] = $i;
			$this->days[$i]['dayOfWeek'] = date('w',$this->day);
			$this->appointment = new Appointment();
			$this->appointments = $this->appointment->getAppointmentsByDayTime($this->day);
			if(!empty($this->appointments))
			{
				$ii = 0;
				foreach($this->appointments as $appointment)
				{
					$this->days[$i]['appointments'][$ii]['id'] = $appointment['id'];
					if(TIME_TYPE == 24)
					{
						$this->days[$i]['appointments'][$ii]['start'] = date('H:i',$appointment['start']);
						$this->days[$i]['appointments'][$ii]['end'] = date('H:i',$appointment['end']);
					}
					elseif(TIME_TYPE == 12)
					{
						$this->days[$i]['appointments'][$ii]['start'] = date('g:i a',$appointment['start']);
						$this->days[$i]['appointments'][$ii]['end'] = date('g:i a',$appointment['end']);
					}
					$ii++;	
				}
			}
		}
		return $this->days;
	}
	
	/*
	*Create array with information for calendar
	*
	*
	*return array: data with appointments in month
	*/
	public function getCalendar()
	{
		$this->days = $this->getMonth();
		if(FIRST_DAY == 'monday')
		{
			$firstDay = 7;
			$lastDay = 0;
		}
		elseif(FIRST_DAY == 'sunday')
		{
			$firstDay = 0;
			$lastDay = 6;
		}

		$count =  $this->days[1]['dayOfWeek'];
		if($count == 0)
		{
			$count = $firstDay;
		}
		if(FIRST_DAY == 'monday')
		{
			for($i = 1; $i < $count; $i++)
			{
				array_unshift($this->days, array());
			}
		}
		elseif(FIRST_DAY == 'sunday')	
		{
			for($i = 1; $i <= $count; $i++)
			{
				array_unshift($this->days, array());
			}	
		}
		unset($count);
		$i = 1;
		foreach($this->days as $array=>$day)
			{
				$this->data['weeks'][$i]['days'][]= $day;
				if(isset($day['dayOfWeek']) && $day['dayOfWeek'] == $lastDay)
				{
					$i++;
				}	
			}
		if(FIRST_DAY == 'monday')
		{	
			$this->data['titleDays'][1]['title'] = 'Monday';
			$this->data['titleDays'][2]['title'] = 'Tuesday';
			$this->data['titleDays'][3]['title'] = 'Wednesday';
			$this->data['titleDays'][4]['title'] = 'Thursday';
			$this->data['titleDays'][5]['title'] = 'Friday';
			$this->data['titleDays'][6]['title'] = 'Saturday';
			$this->data['titleDays'][7]['title'] = 'Sunday';
		}
		elseif(FIRST_DAY == 'sunday')
		{
			$this->data['titleDays'][1]['title'] = 'Sunday';
			$this->data['titleDays'][2]['title'] = 'Monday';
			$this->data['titleDays'][3]['title'] = 'Tuesday';
			$this->data['titleDays'][4]['title'] = 'Wednesday';
			$this->data['titleDays'][5]['title'] = 'Thursday';
			$this->data['titleDays'][6]['title'] = 'Friday';
			$this->data['titleDays'][7]['title'] = 'Saturday';
		}
		return $this->data;
	}
	
	/*
	*Create array with information for calendar navigation
	*
	*
	*return array: array with data for calendar navigation
	*/
	public function getNavigation()
	{
		if($this->month == 1)
		{
			$this->keys['lastyear'] = $this->year - 1;
			$this->keys['lastmonth'] = 12;
			$this->keys['nextmonth'] = $this->month + 1;
			$this->keys['nextyear'] = $this->year;
		} 
		elseif($this->month == 12)
		{
			$this->keys['lastyear'] = $this->year;
			$this->keys['lastmonth'] = $this->month - 1;
			$this->keys['nextmonth'] = 1;
			$this->keys['nextyear'] = $this->year + 1;
		}
		else
		{
			$this->keys['lastyear'] = $this->year;
			$this->keys['lastmonth'] = $this->month - 1;
			$this->keys['nextmonth'] = $this->month + 1;
			$this->keys['nextyear'] = $this->year;	
		}
		
		$this->keys['now'] = date('F Y',$this->monthUnixTime);
		return $this->keys;
	}
}
